package com.neoden.notes.util;

import com.neoden.notes.R;
import com.neoden.notes.data.NotesProvider;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;

public class DialogHelpers 
{
	// name by convention
	private static final String COLUMN_ID = "_id";
	
	public static interface OnConfirmListener {
		public abstract void onConfirm();
	}
	
	public static interface OnSubmitTextListener {
		public abstract void onSubmitText(String text);
	}
	
	public static interface OnSelectListener {
		public abstract void onSelect(long id);
	}
	
	public static void confirm(Context context, String title, String message, 
			final OnConfirmListener onConfirmListener) 
	{
		AlertDialog ad = new AlertDialog.Builder(context).create();
		
		ad.setCancelable(true);
		
		if (title != null) {
			ad.setTitle(title);
		}
		
		if (message != null) {
			ad.setMessage(message);
		}
		
		ad.setButton(
				AlertDialog.BUTTON_POSITIVE, 
				context.getString(R.string.button_yes),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						onConfirmListener.onConfirm();
						dialog.dismiss();
					}
				}				
			);
		
		ad.setButton(
				AlertDialog.BUTTON_NEGATIVE, 
				context.getString(R.string.button_no),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}
			);
		
		ad.show();
	}
	
	public static void submitText(Context context, String title, String text,
			final OnSubmitTextListener onSubmitTextListener) 
	{
		LayoutInflater inflater = LayoutInflater.from(context);
		final EditText editText = (EditText)inflater.inflate(R.layout.alert_set_title, null);
		
		final AlertDialog ad = new AlertDialog.Builder(context).create();

		ad.setCancelable(true);
		
		if (title != null) {
			ad.setTitle(title);
		}
		
		if (text != null) {
			editText.setText(text);
			editText.selectAll();
		}
		
		ad.setView(editText);
		
		ad.setButton(
				AlertDialog.BUTTON_POSITIVE, 
				context.getString(R.string.button_ok),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						onSubmitTextListener.onSubmitText(editText.getText().toString());
						dialog.dismiss();
					}
				}				
			);
		
		ad.setButton(
				AlertDialog.BUTTON_NEGATIVE, 
				context.getString(R.string.button_cancel),
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				}
			);
		
		editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
			public void onFocusChange(View v, boolean hasFocus) {
		        if (hasFocus) {
		            ad.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
		        }
			}
		});
		
		ad.show();
	}

	public static void selectFromList(Context context, String title, final Cursor list,
			final OnSelectListener onSelectListener)
	{
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		
		if (title != null) {
			builder.setTitle(title);
		}

		builder.setCursor(list, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int which) {
				list.moveToPosition(which);
				onSelectListener.onSelect(list.getLong(list.getColumnIndex(COLUMN_ID)));
				dialog.dismiss();
				list.close();
			}
		}, NotesProvider.Lists.COLUMN_TITLE);
		builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
			
			public void onCancel(DialogInterface dialog) {
				list.close();
			}
		});
		
		AlertDialog ad = builder.create();
		ad.setCancelable(true);
		
		ad.show();	
	}
}
